<template>
  <div class="container-login">
    <div class="back" @click="$router.go(-1)">
      <van-icon name="arrow-left" />
    </div>
    <div class="box">
      <div class="title">登录 / 注册</div>
      <van-field v-model="nickname" clearable placeholder="USERNAME" />
      <!-- 密码 -->
      <van-field v-model="password" type="password" placeholder="PASSWORD" />
      <!-- 手机号 -->
      <van-field v-model="phone" type="tel" label="手机号" placeholder="仅注册需要" />
      <!-- 短信验证码 -->
      <van-field v-model="sms" center clearable label="短信验证码" placeholder="仅注册需要">
        <van-button
          class="codeMsg"
          slot="button"
          size="small"
          type="primary"
          @click="getcodeMsg"
          :disabled="flag"
        >{{codeMsg}}</van-button>
      </van-field>
      <div class="codes">
        <van-field
          v-model="verify"
          center
          clearable
          label="验证码"
          placeholder="请输入验证码"
          class="verify"
        />
        <div v-html="code" class="code" @click="getAverify()"></div>
      </div>
      <div class="btn-group">
        <van-button @click="login" class="login-btn" type="primary">登录</van-button>
        <van-button @click="regist" class="login-btn" type="danger">注册</van-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      nickname: "",
      password: "",
      phone: "",
      sms: "",
      verify: "",
      code: "",
      flag: false,
      codeMsg: "发送验证码"
    };
  },
  props: {},
  components: {},
  methods: {
    getAverify() {
      this.$api
        .getAverify()
        .then(res => {
          this.code = res;
        })
        .catch(err => {
          console.log(err);
        });
    },
    login() {
      this.$api
        .login(this.nickname, this.password, this.verify)
        .then(res => {
          if (res.code === 200) {
            localStorage.setItem("nickname", this.nickname);
            this.$store.state.nickname = this.nickname;
            this.$store.state.history = [];
            this.$store.state.browseList = [];
            history.back();
            this.$toast("登录成功");
          } else {
            this.$toast(res.msg);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    regist() {
      this.$api
        .register(this.nickname, this.password, this.verify)
        .then(res => {
          if (res.code === 200) {
            localStorage.setItem("nickname", this.nickname);
            this.$store.state.nickname = this.nickname;
            this.$store.state.history = [];
            this.$store.state.browseList = [];
            history.back();
            this.$toast("注册成功");
          } else {
            this.$toast(res.msg);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    getcodeMsg() {
      let time = 5;
      let timer = setInterval(() => {
        this.codeMsg = `获取中 ${time}`;
        this.flag = true;
        time--;
        if (time < 0) {
          this.flag = false;
          clearInterval(timer);
          this.codeMsg = `重新获取`;
        }
      }, 1000);
    }
  },
  mounted() {
    this.getAverify();
  },
  watch: {},
  computed: {}
};
</script>

<style scoped lang='scss'>
.container-login {
  background: url("../../assets/login.jpg");
  width: 100%;
  height: 100vh;
  overflow: hidden;
}
.back {
  position: fixed;
  left: 5px;
  top: 5px;
  z-index: 10;
  .van-icon-arrow-left {
    font-size: 24px;
    line-height: 40px;
    text-align: center;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    color: rgb(182, 182, 182);
    background: rgba($color: #cecece, $alpha: 0.5);
  }
}
.box {
  margin: 16vh auto;
  padding: 12px;
  width: 330px;
  height: 78vh;
  background: white;
}
.title {
  font-size: 24px;
  padding-bottom: 2vh;
}
.van-field {
  margin-bottom: 3vh;
}
.codeMsg {
  width: 70px;
}
.btn-group {
  margin-top: 20px;
}
.login-btn {
  margin: 0 10px;
  width: 80px;
}
.codes {
  display: flex;
}

.code {
  width: 100px;
  height: 40px;
}
</style>